package body Protocol is


   procedure Get_Line (Socket : in Socket_Type; Text : in out Unbounded_String) is
      S : String(1..1);
      Textlangd : Integer := 1;
   begin
      Text := To_Unbounded_String("");
      Get_Line(Socket, S, Textlangd);
      while (Textlangd /= 0) loop
         Insert(Text, Length(Text)+1, S);
         Get_Line(Socket, S, Textlangd);
      end loop;
   end Get_Line;

   ------------------------------------------------

   procedure Put_Line(Socket : in Socket_Type; Text : in Unbounded_String) is

   begin

      Put_Line(Socket, To_String(Text));

   end Put_Line;

   ------------------------------------------------


   function Create_Message(Header : Character;
                           Content : Unbounded_String) return Unbounded_String is
      Time        : Date_And_Time_Type;
      Temp_String : Unbounded_String;
   begin
      Clock(Time);
      Insert(Temp_String, Length(Temp_String)+1, To_String(Get_Time(Time)) & " "
             & Header & " " & To_String(Content));
      return Temp_String;
   end Create_Message;

   ------------------------------------------------

   function Get_Header(Message : Unbounded_String) return Character is
   begin
      return Element(Message, 10);
   end Get_Header;

   ------------------------------------------------

   function Get_Body(Message : Unbounded_String) return Unbounded_String is
   begin
      return To_Unbounded_String(Slice(Message, 12, Length(Message)));
   end Get_Body;

   ------------------------------------------------

   function Get_Figures(Content : in Unbounded_String) return Integer is
      I       : Integer := 0;
      Counter : Integer := 1;
      C       : Character;
   begin
      loop
         C := Element(Content, Counter);
         exit when C = ' ';
         I := I * 10 + (Character'Pos(C) - Character'Pos('0'));
         Counter := Counter + 1;
      end loop;
      return I;
   end Get_Figures;

   ------------------------------------------------

   function Get_Nth_Figure(Content : in Unbounded_String;
                           N       : in Integer) return String is
      Counter   : Integer := 0;
      C         : Character;
      Low, High : Integer := -1;

   begin

      for I in 1..Length(Content) loop
         C := Element(Content,I);
         if Counter = 2*(N-1) then
            Low := I;
         end if;

         if Counter = 2*N+1 then
            High := I;
            exit;
         end if;

         if C = ' ' then
            Counter := Counter + 1;
         end if;

      end loop;

      if High = -1 then
         High := Length(Content)+2;
      end if;

      return Slice(Content,Low+1,High-2);

   end Get_Nth_Figure;

   ------------------------------------------------

   function Get_Figure_Number(Content : in Unbounded_String) return Integer is
   begin
      return Get_Figures(Content);
   end Get_Figure_Number;

   ------------------------------------------------

   function Get_Solution(Content : in Unbounded_String) return String is
   begin
      return Get_Nth_Figure(Content,1);
   end Get_Solution;

   ------------------------------------------------

   function Get_Traverse(Content : in Unbounded_String;
                          N       : in Integer) return String is
      Counter   : Integer := 0;
      C         : Character;
      Low, High : Integer := -1;

   begin

      for I in 1..Length(Content) loop
         C := Element(Content,I);
         if Counter = 7*(N-1)+1 then
            Low := I;
         end if;

         if Counter = 7*N+1 then
            High := I;
            exit;
         end if;

         if C = ' ' then
            Counter := Counter + 1;
         end if;

      end loop;

      if High = -1 then
         High := Length(Content)+2;
      end if;

      return Slice(Content,Low+1,High-2);

   end Get_Traverse;

   ------------------------------------------------

   function Put_Orientation(List : in Figure_List) return Unbounded_String is

   begin

      if EmptyList(List) then

         return To_Unbounded_String("");

      else

         return " ! " & To_Unbounded_String(Orientation_To_String(FirstFigure(List))) &
           Put_Orientation(RestFigures(List));

      end if;

   end Put_Orientation;

   ------------------------------------------------

   --function Put_Number(

end Protocol;



